//
// Created by vikey on 2022/4/11.
//

#include "QrCodeTools.h"
#include <iostream>
#include <cstring>
#include "zint.h"

bool QrCodeTools::GenerateBarcode(const char *text, int len, const char* outfile)
{
  struct zint_symbol *symbol = nullptr;
  int ret = 0;

  try {
    symbol = ZBarcode_Create();
    if (symbol != nullptr) {
      symbol->symbology = BARCODE_QRCODE; // Symbology to use
      symbol->option_1 = ecc_level_;
      symbol->option_2 = version_;
      symbol->scale = scale_;
      symbol->border_width = 1; // set white space width around your qrcode and 0 is for nothing
      std::strcpy(symbol->outfile, outfile);
      ret = ZBarcode_Encode(symbol, (unsigned char *) text, len); //编码
      if (ret != 0) {
        std::cout << "ZBarcode_Encode err: " << symbol->errtxt << std::endl;
        ZBarcode_Delete(symbol);
        return false;
      }
      ret = ZBarcode_Print(symbol, 0); //antate angle 旋转角度
      std::cout << "ZBarcode_Print ret: " << ret <<
                ", bitmap_width: " << symbol->bitmap_width << ", bitmap_height: " << symbol->bitmap_height << std::endl;

      ZBarcode_Delete(symbol);
      return true;
    } else {
      std::cout << "ZBarcode_Create err: " << symbol->errtxt << std::endl;
      return false;
    }
  } catch (const std::exception &ex) {
    std::cout << "GenerateBarcode exception: " << ex.what() << std::endl;
    return false;
  }
}